package com.lsj.controller;

import com.lsj.core.response.CommonResult;
import com.lsj.core.util.ExceptionHelperUtil;
import com.lsj.dto.sys.RoleDto;
import com.lsj.dto.sys.UserRoleDto;
import com.lsj.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:lin.sj
 * @Data:2021/7/8 19:28
 * @Desc: 用户与角色处理
 */
@RestController
@RequestMapping("/sys_user_role")
@Api(tags = "用户与角色处理")
public class UserRoleController {

    @Autowired
    UserRoleService userRoleService;

    @PostMapping("/updateRoleForUser")
    @ApiOperation(value = "更新用户与角色关系")
    public CommonResult updateRoleForUser(@RequestBody @Validated UserRoleDto dto){
        userRoleService.updateRoleForUser(dto);
        return CommonResult.success();
    }

    @GetMapping("/findRoleByUserId")
    @ApiOperation(value = "根据用户ID获取关联的用户角色")
    public CommonResult findRoleByUserId(@RequestParam("userId") Long userId){
        ExceptionHelperUtil.nullObjException(userId,"用户主键不能为空");
        List<RoleDto> roles = userRoleService.findRoleByUserId(userId);
        return CommonResult.success(roles);
    }
}
